package com.cpypst;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.cpypst.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

public class CameraActivity extends Activity {
	private static final String TAG = "CameraDemo";
	protected static final int SEE_LIST = 1;
	Preview preview; // <1>
	Button btnSrc; // <2>
	Button btnDest;
	Button btnGoList;
	TextView tvT;
	TextView tvST;

	Context c;



	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		preview = new Preview(this); // <3>
		((FrameLayout) findViewById(R.id.preview)).addView(preview); // <4>

		btnSrc = (Button) findViewById(R.id.buttonSource);
		btnDest = (Button) findViewById(R.id.buttonDest);
		btnGoList = (Button) findViewById(R.id.buttonGoList);


		btnSrc.setOnClickListener(new OnClickListener() {
			public void onClick(View v) { // <5>
				preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback );
			}
		});

		btnDest.setOnClickListener(new OnClickListener() {
			public void onClick(View v) { // <5>
				preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
			}
		});
		
		btnGoList.setOnClickListener(new OnClickListener() {
			public void onClick(View v) { // <5>
				//Intent goToList = new Intent(CameraActivity.this.getApplicationContext(), ListeActivity.class);
				//goToList.putExtra("value1", 1);
				//startActivity(goToList);
				System.out.println("OK");
			}
		});



		Log.d(TAG, "onCreate'd");

	}


	// Called when shutter is opened
	ShutterCallback shutterCallback = new ShutterCallback() { // <6>
		public void onShutter() {
			Log.d(TAG, "onShutter'd");
		}
	};

	//Handles data for raw picture
	PictureCallback rawCallback = new PictureCallback() { // <7>
		public void onPictureTaken(byte[] data, Camera camera) {
			Log.d(TAG, "onPictureTaken - raw");
		}
	};

	// Handles data for jpeg picture
	PictureCallback jpegCallback = new PictureCallback() { // <8>
		public void onPictureTaken(byte[] data, Camera camera) {
			FileOutputStream outStream = null;
			try {
				// Write to SD Card
				outStream = new FileOutputStream("source.jpg"); // <9>
				// send to server
				outStream.write(data);
				outStream.close();
				Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
			} catch (FileNotFoundException e) { // <10>
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
			}
			Log.d(TAG, "onPictureTaken - jpeg");
		}
	};


}
